package cn.lf.android.lawfirm.changeapproval;

import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.BaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.changeapproval.controller.ChangeLawyerFeeInfoController;
import cn.lf.android.lawfirm.changeapproval.controller.ChangeLawyerFeeListController;
import cn.lf.android.lawfirm.changeapproval.modle.ChangeLawyerFeeInfo;
import cn.lf.android.lawfirm.changeapproval.view.BeforeLawyerFeeAdapter;
import cn.lf.android.lawfirm.undertake.approval.LawyerFeeListActivity;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFee;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFeeInfo;
import cn.lf.android.lawfirm.undertake.approval.view.LawyerFeeAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;

public class BeforeCostDetailActivity extends BaseActivity implements ChangeLawyerFeeInfoController.ChangeBeforeFeeInfoCallback, ChangeLawyerFeeListController.ChangeFeeListCallback {

    /**
     * 返回
     **/
    private ImageButton img_left;
    /**
     * 标题
     **/
    private TextView mtitle;
    private AutoRelativeLayout rl_fee;
    private String approvesid = "";
    private ChangeLawyerFeeInfoController mLawyerFeeInfoController;
    /**
     * 总金额
     **/
    private TextView total;
    /**
     * 付款方式
     **/
    private TextView payment;
    /**
     * 收费方式
     **/
    private TextView charging_mode;
    /**
     * 费用
     **/
    private ListView mListView;
    private BeforeLawyerFeeAdapter mLawyerFeeAdapter;
    private List<LawyerFee> list = new ArrayList<LawyerFee>();
    private ChangeLawyerFeeListController mLawyerFeeListController;
    private ScrollView scrollview;
    private AutoRelativeLayout hetong, shoufei, fukuan;

    @Override
    public Object getContentResView() {
        return R.layout.activity_cost_detail;
    }

    @Override
    public void initView() {
        initTitle();
        approvesid = getIntent().getStringExtra("approvesid");
        hetong = (AutoRelativeLayout) findViewById(R.id.hetong);
        hetong.setVisibility(View.GONE);
        shoufei = (AutoRelativeLayout) findViewById(R.id.shoufei);
        shoufei.setVisibility(View.GONE);
        fukuan = (AutoRelativeLayout) findViewById(R.id.fukuan);
        fukuan.setVisibility(View.GONE);
        rl_fee = (AutoRelativeLayout) findViewById(R.id.rl_fee);
        rl_fee.setOnClickListener(this);
        total = (TextView) findViewById(R.id.total);
        payment = (TextView) findViewById(R.id.payment_method);
        charging_mode = (TextView) findViewById(R.id.charging_mode);
        mListView = (ListView) findViewById(R.id.lv_list);
        mLawyerFeeAdapter = new BeforeLawyerFeeAdapter(this, list);
        mListView.setAdapter(mLawyerFeeAdapter);
        scrollview = (ScrollView) findViewById(R.id.scrollview);
        scrollview.smoothScrollTo(0, 20);
    }


    private void initTitle() {
        img_left = (ImageButton) findViewById(R.id.img_left);
        img_left.setVisibility(View.VISIBLE);
        img_left.setOnClickListener(this);
        mtitle = (TextView) findViewById(R.id.eb_tv_title);
        mtitle.setVisibility(View.VISIBLE);
        mtitle.setText("费用详情");
    }

    @Override
    public void initData() {
        //滑动退出
//        addSwipeFinishLayout();
        mLawyerFeeInfoController = new ChangeLawyerFeeInfoController(this, this);
        //先请求列表 用信息的SID做参数
        mLawyerFeeListController = new ChangeLawyerFeeListController(this, this);
        mLawyerFeeListController.onFeeList(approvesid);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_left:
                finish();
                break;
            case R.id.rl_fee:
                Intent intent1 = new Intent(this, LawyerFeeListActivity.class);
                intent1.putExtra("feelist", mbeans);
                startActivity(intent1);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    LawyerFeeInfo mbeans;

    @Override
    public void onChangeBeforeFeeInfoSuccessed(LawyerFeeInfo beans) {
        i++;
        if (beans.Payment.size() > 0) {
            beans.Payment.get(0).isFirst = "0";
            beans.Payment.get(0).hetong = beans.contractAmount;
            beans.Payment.get(0).shoufei = beans.chargeType;
            beans.Payment.get(0).fukuan = beans.payWay;
            list.addAll(beans.Payment);
        } else {
            total.setText(beans.contractAmount);
            payment.setText(beans.payWay);
            charging_mode.setText(beans.chargeType);
            hetong.setVisibility(View.VISIBLE);
            shoufei.setVisibility(View.VISIBLE);
            fukuan.setVisibility(View.VISIBLE);
        }
        if (i < mBean.size()) {
            mLawyerFeeInfoController.onBeforeFeeInfo(mBean.get(i).SID);
        } else {
            mLawyerFeeAdapter.notifyDataSetChanged();
        }
//        mbeans = beans;
//        DebugLog.e("999==" + beans);
//        total.setText(beans.contractAmount);
//        payment.setText(beans.payWay);
//        charging_mode.setText(beans.chargeType);
//        list.addAll(beans.Payment);
//        mLawyerFeeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onChangeBeforeFeeInfoFailed() {

    }

    private List<ChangeLawyerFeeInfo> mBean = new ArrayList<ChangeLawyerFeeInfo>();
    private int i = 0;

    @Override
    public void onChangeFeeListSuccessed(List<ChangeLawyerFeeInfo> beans) {
        mBean.addAll(beans);
        if (beans.size() > 0) {
            mLawyerFeeInfoController.onBeforeFeeInfo(mBean.get(i).SID);
        }
    }

    @Override
    public void onChangeFeeListFailed() {

    }
}
